package com.xrtn.controller;

import com.xrtn.ApiVersion;
import com.xrtn.bean.TOrderGoodsDetail;
import com.xrtn.bean.TOrderInfo;
import com.xrtn.bean.TOrderServiceDetail;
import com.xrtn.service.IDoctorService;
import com.xrtn.utils.CodeEnum;
import com.xrtn.utils.ObjUtils;
import com.xrtn.utils.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.aspectj.apache.bcel.classfile.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tu_pei on 2018/3/1.
 */
@Api("订单数据模块")
@Controller
@RequestMapping("/{version}/order")
public class OrderController {

    @Autowired
    private IDoctorService doctorService;

    /**
     * @Title: saveOrderInfo
     * @Description: 保存订单
     * @param @param userId
     * @param @return
     * @return ResponseData
     * @throws
     */
    @ApiOperation(value = "保存订单-开始调理",notes="保存订单-开始调理")
    @RequestMapping(value = "/saveOrderInfo",method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="bookingId",dataType="int",required=true,value="预约id",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="doctorId",dataType="string",required=true,value="医师id",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="subclinicId",dataType="int",required=true,value="分店id",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="patientId",dataType="int",required=true,value="患者id",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="userId",dataType="int",required=true,value="家长id",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="orderTotalCost",dataType="double",required=true,value="应付金额",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="orderPayCost",dataType="double",required=true,value="实付金额",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="goodsList",dataType="array",required=true,value="药品数组",defaultValue = "[{\"goodsId\":1,\"goodsName\":\"12\",\"goodsImg\":\"12\",\"goodsCost\":12.12,\"goodsIncome\":1.2,\"orderNum\":2}]"),
            @ApiImplicitParam(paramType="query",name="serviceList",dataType="array",required=true,value="推拿服务数组",defaultValue = "[{\"serviceId\":1,\"serviceName\":\"12\",\"serviceImg\":\"12\",\"serviceCost\":12.12,\"serviceIncome\":12.12}]"),
    })
    @ApiVersion(1)
    @ResponseBody
    public ResponseData saveOrderInfo(TOrderInfo orderInfo, String goodsList, String serviceList){

            // 创建响应Model
            ResponseData data = new ResponseData();
            // 调用Service
//        563594
            CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{orderInfo.getBookingId(),
                    orderInfo.getUserId(),orderInfo.getDoctorId(),orderInfo.getSubclinicId(),orderInfo.getPatientId(),
                    orderInfo.getOrderTotalCost(),orderInfo.getOrderPayCost(),orderInfo.getCouponTotalCost()
            });
            orderInfo.setCreateTime(new Date());
            orderInfo.setOrderState(2);
        if(ObjUtils.isNotEmpty(codeEnum)){
            data.setCodeEnum(codeEnum);
            return data;
        }

        Integer count = doctorService.saveOrderInfo(orderInfo, goodsList, serviceList);
        if(count > 0 ){
            data.setCodeEnum(CodeEnum.SUCCESS);
            return data;
        }
        if (count == 0){
            data.setCodeEnum(CodeEnum.CODE_30002);
            return data;
        } else if(count == -1) {
            data.setCodeEnum(CodeEnum.CODE_30005);
        } else if(count == -2) {
            data.setCodeEnum(CodeEnum.CODE_30004);
        } else if(count == -3) {
            data.setCodeEnum(CodeEnum.CODE_30006);
        }
        return data;
    }
    /**
     * @Title: updateOrderInfo
     * @Description: 修改订单明细
     * @param @param userId
     * @param @return
     * @return ResponseData
     * @throws
     */
    @ApiOperation(value = "修改订单明细",notes="修改订单明细")
    @RequestMapping(value = "/updateOrderInfo",method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="orderId",dataType="int",required=true,value="订单id",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="bookingId",dataType="int",required=false,value="预约id",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="doctorId",dataType="string",required=false,value="医师id",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="subclinicId",dataType="int",required=false,value="分店id",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="patientId",dataType="int",required=false,value="患者id",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="userId",dataType="int",required=false,value="家长id",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="orderTotalCost",dataType="double",required=false,value="应付金额",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="orderPayCost",dataType="double",required=false,value="实付金额",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="goodsList",dataType="array",required=false,value="药品数组",defaultValue = "[{\"goodsId\":1,\"goodsName\":\"12\",\"goodsImg\":\"12\",\"goodsCost\":12.12,\"goodsIncome\":1.2,\"orderNum\":2}]"),
            @ApiImplicitParam(paramType="query",name="serviceList",dataType="array",required=false,value="推拿服务数组",defaultValue = "[{\"serviceId\":1,\"serviceName\":\"12\",\"serviceImg\":\"12\",\"serviceCost\":12.12,\"serviceIncome\":12.12}]"),
    })
    @ApiVersion(1)
    @ResponseBody
    public ResponseData updateOrderInfo(TOrderInfo orderInfo, String goodsList, String serviceList){

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service

        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{orderInfo.getBookingId(),
                orderInfo.getUserId(),orderInfo.getDoctorId(),orderInfo.getSubclinicId(),orderInfo.getPatientId(),
                orderInfo.getOrderTotalCost(),orderInfo.getOrderPayCost(),orderInfo.getCouponTotalCost()
        });
        if(ObjUtils.isNotEmpty(codeEnum)){
            data.setCodeEnum(codeEnum);
            return data;
        }

        Integer result = doctorService.updateOrderDetail(orderInfo, goodsList, serviceList);
        if (result == -1) { // 已完成订单无法修改
            data.setCodeEnum(CodeEnum.CODE_30014);
            return data;
        }
        data.setResults(result);
        // 返回数据
        return data;
    }

    /**
     * @Title: selectOrderInfo
     * @Description: 查询订单
     * @param @param userId
     * @param @return
     * @return ResponseData
     * @throws
     */
    @ApiOperation(value = "查询订单",notes="查询订单")
    @RequestMapping(value = "/selectOrderInfo",method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="bookingId",dataType="int",required=true,value="预约id",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="patientId",dataType="int",required=true,value="患者id",defaultValue = "1"),
    })
    @ApiVersion(1)
    @ResponseBody
    public ResponseData selectOrderInfo(Integer bookingId,Integer patientId){

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service

        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{ bookingId, patientId});

        if(ObjUtils.isNotEmpty(codeEnum)){
            data.setCodeEnum(codeEnum);
            return data;
        }

        data.setResults(doctorService.selectOrderInfo(bookingId, patientId));
        // 返回数据
        return data;
    }

    /**
     * @Title: selectOrderDetail
     * @Description: 查询订单详情
     * @param @param userId
     * @param @return
     * @return ResponseData
     * @throws
     */
    @ApiOperation(value = "查询订单详情",notes="查询订单详情")
    @RequestMapping(value = "/selectOrderDetail",method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="bookingId",dataType="int",required=false,value="预约id",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="patientId",dataType="int",required=false,value="患者id",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="orderId",dataType="int",required=false,value="订单id",defaultValue = "1"),
    })
    @ApiVersion(1)
    @ResponseBody
    public ResponseData selectOrderDetail(Integer bookingId,Integer patientId,Integer orderId){

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service
        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{ bookingId, patientId,orderId});
        if(ObjUtils.isNotEmpty( codeEnum )){
            //返回空
            data.setCodeEnum(codeEnum);
            return data;
        }
        // 返回数据
        return doctorService.selectOrderDetail(bookingId, patientId, orderId, data);
    }

    /**
     * @Title: closeOrderInfo
     * @Description: 取消订单
     * @param @param userId
     * @param @return
     * @return ResponseData
     * @throws
     */
    @ApiOperation(value = "取消订单",notes="取消订单")
    @RequestMapping(value = "/closeOrderInfo",method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="bookingId",dataType="int",required=true,value="预约id",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="patientId",dataType="string",required=true,value="患者id",defaultValue = "1"),
    })
    @ApiVersion(1)
    @ResponseBody
    public ResponseData closeOrderInfo(Integer bookingId,Integer patientId){

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service

        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{ bookingId, patientId });

        if(ObjUtils.isNotEmpty( codeEnum )){
            data.setCodeEnum(codeEnum);
            return data;
        }

        int status = doctorService.updateOrderInfo(bookingId, patientId, 4);

        if (status != 1) {
            data.setCodeEnum(CodeEnum.CODE_60025);
            return data;
        }
        // 返回数据
        return data;
    }

    /**
     * @Title: completeOrderInfo
     * @Description: 结束诊疗
     * @param @param userId
     * @param @return
     * @return ResponseData
     * @throws
     */
    @ApiOperation(value = "结束诊疗",notes="结束诊疗")
    @RequestMapping(value = "/completeOrderInfo",method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="bookingId",dataType="int",required=true,value="预约id",defaultValue = "1"),
            @ApiImplicitParam(paramType="query",name="patientId",dataType="string",required=true,value="医师id",defaultValue = "1"),
    })
    @ApiVersion(1)
    @ResponseBody
    public ResponseData completeOrderInfo(Integer bookingId,Integer patientId){

        // 创建响应Model
        ResponseData data = new ResponseData();
        // 调用Service

        CodeEnum codeEnum = ObjUtils.validateParams(new Object[]{ bookingId, patientId });

        if(ObjUtils.isNotEmpty(codeEnum)){
            data.setCodeEnum(codeEnum);
            return data;
        }

        Integer count = doctorService.updateOrderInfo(bookingId, patientId, 3);

        if(count > 0){
            return data;
        }
        if(count == -1){
            data.setCodeEnum(CodeEnum.CODE_60019);
            return data;
        }
        if(count == -2){
            data.setCodeEnum(CodeEnum.CODE_60020);
            return data;
        }
        return data;
    }
}
